package com.guiji.common.config;

import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @program: cms-vue-plus
 * @description:
 * @author: gaoX
 * @create: 2021-07-06 15:33
 **/
@Configuration
public class ElasticsearchConfig {
	@Value("${elasticsearch.hostlist}")
	private String hostlist;

	@Value("${elasticsearch.username}")
	private String userName;

	@Value("${elasticsearch.password}")
	private String password;

	@Bean
	public RestHighLevelClient restHighLevelClient(){
		//解析hostlist配置信息
		String[] split = hostlist.split(",");
		//创建HttpHost数组，其中存放es主机和端口的配置信息
		HttpHost[] httpHostArray = new HttpHost[split.length];
		for(int i = 0; i<split.length; i++){
			String item = split[i];
			httpHostArray[i] = new HttpHost(item.split(":")[0], Integer.parseInt(item.split(":")[1]), "http");
		}


		final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
		credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(userName, password));

		// 构建连接对象
		RestClientBuilder builder = RestClient.builder(httpHostArray);
		// 异步连接延时配置
		builder.setRequestConfigCallback(requestConfigBuilder -> {
			return requestConfigBuilder;
		});
		// 异步连接数配置
		builder.setHttpClientConfigCallback(httpClientBuilder -> {
			return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
		});
		return new RestHighLevelClient(builder);
		// return new RestHighLevelClient(RestClient.builder(httpHostArray));
	}
}
